﻿/*
 Copyright (c) 2011.  Dirk Gordon

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace File_CRC32_Verifier_WPF
{
    /// <summary>
    /// This class is unused until I fix the problems with context menus.
    /// </summary>
    public partial class OptionsWindow : Window
    {
        #region Constants
        private const string CRC32 = "CRC32";

        private const string folderMenuName = "Software\\Classes\\Folder\\shell\\CRC32Verifier";
        private const string folderCommand  = "Software\\Classes\\Folder\\shell\\CRC32Verifier\\command";
        private const string allMenuName    = "Software\\Classes\\*\\shell\\CRC32Verifier";
        private const string allCommand     = "Software\\Classes\\*\\shell\\CRC32Verifier\\command";

        private const string filetype       = "Software\\Classes\\.sfv";
        private const string CRC32Verifier  = "Software\\Classes\\CRC32Verifier.sfv";
        private const string CRC32Shell     = "Software\\Classes\\CRC32Verifier.sfv\\shell";
        private const string CRC32MenuName  = "Software\\Classes\\CRC32Verifier.sfv\\shell\\open";
        private const string CRC32Command   = "Software\\Classes\\CRC32Verifier.sfv\\shell\\open\\command";
        #endregion

        /// <summary>
        /// Setup window.
        /// </summary>
        public OptionsWindow()
        {
            InitializeComponent();
        }

        #region Folder
        /// <summary>
        /// Add CRC32 Verifier to folder context menus.
        /// </summary>
        private void btnAddFolder_Click(object sender, RoutedEventArgs e)
        {
            add(folderMenuName, folderCommand, CRC32, System.Reflection.Assembly.GetExecutingAssembly().Location + " \"%1\"");
        }

        /// <summary>
        /// Remove CRC32 Verifier from folder context menus.
        /// </summary>
        private void btnRemoveFolder_Click(object sender, RoutedEventArgs e)
        {
            remove(folderMenuName, folderCommand);
        }
        #endregion

        #region All
        /// <summary>
        /// Add CRC32 Verifier to all context menus.
        /// </summary>
        private void btnAddAll_Click(object sender, RoutedEventArgs e)
        {
            add(allMenuName, allCommand, CRC32, System.Reflection.Assembly.GetExecutingAssembly().Location + " \"%1\"");
        }

        /// <summary>
        /// Remove CRC32 Verifier from all context menus.
        /// </summary>
        private void btnRemoveAll_Click(object sender, RoutedEventArgs e)
        {
            remove(allMenuName, allCommand);
        }
        #endregion

        #region Associate
        /// <summary>
        /// Associate CRC32 Verifier with .sfv files.
        /// </summary>
        private void btnAssociate_Click(object sender, RoutedEventArgs e)
        {
            add(filetype, CRC32Verifier, "CRC32Verifier.sfv", "SFV File");
            add(CRC32MenuName, CRC32Command, "", System.Reflection.Assembly.GetExecutingAssembly().Location + " \"%1\"");
        }

        /// <summary>
        /// Dissociate CRC32 Verifier with .sfv files.
        /// </summary>
        private void btnDissociate_Click(object sender, RoutedEventArgs e)
        {
            remove(CRC32MenuName, CRC32Command);
            remove(CRC32Verifier, CRC32Shell);
            remove(filetype, filetype);
        }
        #endregion

        /// <summary>
        /// Adds the passed registry keys.
        /// </summary>
        /// <param name="menu">The registry menu location.</param>
        /// <param name="command">The registry command location.</param>
        /// <param name="keyMenu">The value to write to the menu.</param>
        /// <param name="keyCommand">The value to write to the command.</param>
        private void add(string menu, string command, string keyMenu, string keyCommand)
        {
            RegistryKey regmenu = null;
            RegistryKey regcmd = null;
            try
            {
                regmenu = Registry.CurrentUser.CreateSubKey(menu);
                if (regmenu != null)
                    regmenu.SetValue("", keyMenu);
                regcmd = Registry.CurrentUser.CreateSubKey(command);
                if (regcmd != null)
                    regcmd.SetValue("", keyCommand);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString());
            }
            finally
            {
                if (regmenu != null)
                    regmenu.Close();
                if (regcmd != null)
                    regcmd.Close();
            }
        }

        /// <summary>
        /// Removes the passed registry keys.
        /// </summary>
        /// <param name="menu">The registry menu location.</param>
        /// <param name="command">The registry command location.</param>
        private void remove(string menu, string command)
        {
            try
            {
                RegistryKey reg = Registry.CurrentUser.OpenSubKey(command);
                if (reg != null)
                {
                    reg.Close();
                    Registry.CurrentUser.DeleteSubKey(command);
                }
                reg = Registry.CurrentUser.OpenSubKey(menu);
                if (reg != null)
                {
                    reg.Close();
                    Registry.CurrentUser.DeleteSubKey(menu);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString());
            }
        }

        /// <summary>
        /// Opens the File CRC32 Verifier website.
        /// </summary>
        private void btnWebsite_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/file-crc32-verifier/");
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        private void btnOkay_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
